package com.hertz.iris.view.setting;

import java.io.IOException;

import javax.inject.Inject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.actionbarsherlock.view.Menu;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
import com.github.kevinsawicki.wishlist.Toaster;
import com.hertz.iris.IrisApplication;
import com.hertz.iris.R;
import com.hertz.iris.core.MatchAlarmService;
import com.hertz.iris.core.VersionService;
import com.hertz.iris.pojo.IrisVersion;
import com.hertz.iris.pojo.MatchNba;
import com.hertz.iris.pojo.ResponseIsAlert;
import com.hertz.iris.pojo.Team;
import com.hertz.iris.ui.IrisFragmentActivity;
import com.hertz.iris.util.Constants;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;

public class SettingActivity extends IrisFragmentActivity{

	@InjectView(R.id.notice_setting_ll)
	LinearLayout noticeSettingLL;
	@InjectView(R.id.notice_setting_tv)
	TextView noticeSettingTV;
	@InjectView(R.id.notice_setting_double_right)
	TextView nsdr;
	
	@InjectView(R.id.sync_data_ll)
	LinearLayout syncDataLL;
	@InjectView(R.id.sync_data_tv)
	TextView syncDataTV;
	
	@InjectView(R.id.other_openid_ll)
	LinearLayout otherOpenidLL;
	@InjectView(R.id.other_openid_tv)
	TextView otherOpenidTV;
	@InjectView(R.id.other_openid_double_right)
	TextView oodr;
	
	@InjectView(R.id.feedback_ll)
	LinearLayout feedBackLL;
	@InjectView(R.id.feedback_tv)
	TextView feedBackTV;
	@InjectView(R.id.feedback_double_right)
	TextView fdr;
	
	@InjectView(R.id.about_version_ll)
	LinearLayout aboutVersionLL;
	@InjectView(R.id.about_version_tv)
	TextView aboutVersionTV;
	@InjectView(R.id.about_version_double_right)
	TextView avdr;
	
	@Inject MatchAlarmService matchAlarmService;
	@Inject VersionService versionService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		ButterKnife.inject(this);
		
		String doubleRight = getResources().getString(R.string.icon_double_angle_right);
		noticeSettingTV.setText(getResources().getString(R.string.notice_setting));
		nsdr.setText(doubleRight);
		nsdr.setTypeface(IrisApplication.getInstance().getTypeface());
		
		syncDataTV.setText(getResources().getString(R.string.sync_data));
		
		otherOpenidTV.setText(getResources().getString(R.string.other_open_id));
		oodr.setText(doubleRight);
		oodr.setTypeface(IrisApplication.getInstance().getTypeface());
		
		feedBackTV.setText(getResources().getString(R.string.feedback) );
		fdr.setText(doubleRight);
		fdr.setTypeface(IrisApplication.getInstance().getTypeface());
		
		aboutVersionTV.setText(getResources().getString(R.string.about_version));
		avdr.setText(doubleRight);
		avdr.setTypeface(IrisApplication.getInstance().getTypeface());
	}

	@OnClick(R.id.other_openid_ll)
	public void otherOpenIdClick(){
		ResponseIsAlert ria = new ResponseIsAlert();
		Team guestTeam = new Team();
		guestTeam.setTeamName("aa");
		Team hTeam = new Team();
		hTeam.setTeamName("bb");
		ria.setGuestTeam(guestTeam);
		ria.setHostTeam(hTeam);
		ria.setIrisNbaAlarm(matchAlarmService.getMatchAlarmFromLocal(618));
		MatchNba matchNba = new MatchNba();
		matchNba.setGuestScore(Short.valueOf("10"));
		matchNba.setHostScore(Short.valueOf("11"));
		matchNba.setStatus("test");
		matchNba.setId(6181L);
		ria.setMatchNba(matchNba);
		matchAlarmService.createNotication(ria, this);
	}
	
	@OnClick(R.id.feedback_ll)
	public void feedbackClick(){
		FeedbackAgent feedbackAgent = new FeedbackAgent(this);
		feedbackAgent.startFeedbackActivity();
	}
	
	@OnClick(R.id.sync_data_ll)
	public void syncDataClick(){
		Runnable runnable = new SyncDataRunnable();
		new Thread(runnable).start();
	}
	private Handler syncDataHandler = new Handler(){
		public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case -1:
            	Toaster.showShort(SettingActivity.this, Constants.ToastMessage.NETWORK_ERROR);
            	break;
            case 1:  
            	Toaster.showShort(SettingActivity.this, Constants.ToastMessage.SYNC_SUCCESS);
                break;
            case 0:
            	Toaster.showShort(SettingActivity.this, Constants.ToastMessage.SYNC_ERROR);
            }  
        }; 
	};
	private class SyncDataRunnable implements Runnable{
		@Override
		public void run() {
			Message message = new Message();  
            try {
            	boolean result = matchAlarmService.syncData(Constants.DEFAULT_SYNC_TIME_DAYS, SettingActivity.this);
            	if(result){
            		message.what = 1;
            	}
			}catch (HttpRequestException hre){
				hre.printStackTrace();
				message.what = -1;
			} catch (IOException ioe){
				ioe.printStackTrace();
				message.what = 0;
			} 
            
            SettingActivity.this.syncDataHandler.sendMessage(message);
		}
	}
	
	@OnClick(R.id.about_version_ll)
	public void checkVersion(){
		UmengUpdateAgent.setUpdateAutoPopup(false);
		UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
		    @Override
		    public void onUpdateReturned(int updateStatus,UpdateResponse updateInfo) {
		        switch (updateStatus) {
		        case UpdateStatus.Yes: // has update
		            UmengUpdateAgent.showUpdateDialog(SettingActivity.this, updateInfo);
		            break;
		        case UpdateStatus.No: // has no update
		        	Toaster.showShort(SettingActivity.this, "没有更新");
		            break;
		        case UpdateStatus.NoneWifi: // none wifi
		        	Toaster.showShort(SettingActivity.this, "没有wifi连接， 只在wifi下更新");
		            break;
		        case UpdateStatus.Timeout: // time out
		        	Toaster.showShort(SettingActivity.this, "超时");
		            break;
		        }
		    }
		});
		UmengUpdateAgent.forceUpdate(this);
	}
	
	
	private Handler checkVersionHandler = new Handler(){
		public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case -1:
            	Toaster.showShort(SettingActivity.this, Constants.ToastMessage.NETWORK_ERROR);
            	break;
            case 1:  
            	Bundle bundle = msg.getData();
            	IrisVersion irisVersion = (IrisVersion)bundle.getSerializable("irisVersion");
            	if(irisVersion == null)
            		Toaster.showShort(SettingActivity.this, Constants.ToastMessage.VERSION_WAS_NEW);
            	else{
            		versionService.showNoticeDialog(SettingActivity.this, irisVersion);
            	}
                break;
            case 0:
            	Toaster.showShort(SettingActivity.this, Constants.ToastMessage.SYNC_ERROR);
            }  
        }; 
	};
	private class checkVersionRunnable implements Runnable{
		@Override
		public void run() {
			Message message = new Message();  
            try {
            	IrisVersion irisVersion = versionService.checkVersion(SettingActivity.this);
            	Bundle bundle = new Bundle();
            	bundle.putSerializable("irisVersion", irisVersion);
            	message.setData(bundle);
            	message.what = 1;
			}catch (HttpRequestException hre){
				hre.printStackTrace();
				message.what = -1;
			} catch (IOException ioe){
				ioe.printStackTrace();
				message.what = 0;
			} 
            
            SettingActivity.this.checkVersionHandler.sendMessage(message);
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
}
